package com.sgb.kjwl.view.ui.fragment.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.bumptech.glide.Glide;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentTitleItem1Binding;
import com.swgk.core.view.entity.MenuEntity;
import com.sgb.kjwl.view.ui.adapter.home.TitleItemAdapter;
import com.swgk.core.base.di.AppComponent;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
public class TitleItemFragment1 extends LazyFragment2 {

    private static final int ITEM_COUNT = 3;
    private FragmentTitleItem1Binding binding;

    private List<MenuEntity> menuEntities = new ArrayList<>();
    private TitleItemAdapter.OnItemClick<MenuEntity> onItemClick;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_title_item_1, container, false);
        initView();
        return binding.getRoot();
    }

    @Override
    protected void onMultiClick(View v) {
        if (onItemClick == null || menuEntities.size() < 3) {
            return;
        }
        switch (v.getId()) {
            case R.id.ll_container_0:
                onItemClick.onItemClick(menuEntities.get(0), 0);
                break;
            case R.id.ll_container_1:
                onItemClick.onItemClick(menuEntities.get(1), 1);
                break;
            case R.id.ll_container_2:
                onItemClick.onItemClick(menuEntities.get(2), 2);
                break;
            default:
                break;
        }
    }

    @Override
    protected void initView() {
        binding.llContainer0.setOnClickListener(this);
        binding.llContainer1.setOnClickListener(this);
        binding.llContainer2.setOnClickListener(this);

    }

    public void loadData(List<MenuEntity> menuEntities) {
        if (menuEntities != null && menuEntities.size() >= ITEM_COUNT) {
            this.menuEntities = menuEntities;

            Glide.with(context).load(menuEntities.get(0).getAppIcon()).into(binding.imageView0);
            binding.tvName0.setText(menuEntities.get(0).getName());
//            binding.imageView1.setBackground(context.getResources().getDrawable(R.mipmap.icn_map_find));
            Glide.with(context).load(menuEntities.get(1).getAppIcon()).into(binding.imageView1);
//            RoundedCorners roundedCorners = new RoundedCorners(20);
//            //4.9.0
//            Glide.with(context)
//                    .load(menuEntities.get(1).getAppIcon())
//                    .transform(new MultiTransformation(new CenterCrop(), roundedCorners))
//                    .into(binding.imageView1);
            //4.8.0
//            MultiTransformation multiTransformation = new MultiTransformation<>(new CenterCrop(), roundedCorners);
//            RequestOptions options = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.DATA).bitmapTransform(multiTransformation);
//            Glide.with(context)
//                    .load(menuEntities.get(1).getAppIcon())
//                    .apply(options)
//                    .into(binding.imageView1);
            Glide.with(context).load(menuEntities.get(2).getAppIcon()).into(binding.imageView2);
            binding.tvName2.setText(menuEntities.get(2).getName());
        }
    }


    public void setOnItemClick(TitleItemAdapter.OnItemClick<MenuEntity> onItemClick) {
        this.onItemClick = onItemClick;
    }

}
